package com.bocai.yuweiwangluo.activity;


import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.View.XListView;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.bean.GameListBean;
import com.bocai.yuweiwangluo.bean.PopBean;
import com.bocai.yuweiwangluo.presenter.GameListPresenter;
import com.bocai.yuweiwangluo.utils.PopUtil;
import com.bocai.yuweiwangluo.viewModel.IGameListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

/**
 * 比赛列表界面
 */
public class GameActivity extends BaseActivity implements View.OnClickListener,IGameListView,XListView.IXListViewListener,AdapterView.OnItemClickListener
{
    @ViewInject(R.id.ll_nocotent)
    LinearLayout llnoContent;
    @ViewInject(R.id.lv_game_list)
    XListView lv_game_list;
    @ViewInject(R.id.ll_game_sporttype)
    LinearLayout ll_game_sporttype;
    @ViewInject(R.id.ll_game_area)
    LinearLayout ll_game_area;
    @ViewInject(R.id.tv_game_sporttype)
    TextView tv_game_sporttype;
    @ViewInject(R.id.tv_game_area)
    TextView tv_game_area;
    @ViewInject(R.id.iv_game_sporttype)
    ImageView iv_game_sporttype;
    @ViewInject(R.id.iv_game_area)
    ImageView iv_game_area;
    @ViewInject(R.id.ll_refresh)
    LinearLayout llRefresh;
    private int width;
    private PopUtil pu;
    GameListPresenter mGameListPresenter;
    GameListBean gameListBean;
    private ArrayList<PopBean> list;
    private String type_id="0";
    private String city_id="0";
    private int page=1;
    @Override
    protected int contentView()
    {
        return R.layout.activity_game;
    }

    @Override
    protected void setData()
    {
        ViewUtils.inject(this);
        WindowManager windowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        width=windowManager.getDefaultDisplay().getWidth() / 2;
        initToolbar("比赛");
        getGameListPresenter();
        mGameListPresenter.getGameList(page+"",type_id,city_id);
        initEvent();
    }

    @Override
    protected void initEvent()
    {
        super.initEvent();
        ll_game_sporttype.setOnClickListener(this);
        ll_game_area.setOnClickListener(this);
        llRefresh.setOnClickListener(this);
        lv_game_list.setPullLoadEnable(true);
        lv_game_list.setPullRefreshEnable(true);
        lv_game_list.setXListViewListener(this);
        lv_game_list.setOnItemClickListener(this);
    }


    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ll_refresh:
                llRefresh.setVisibility(View.GONE);
                onRefresh();
                break;
            case R.id.ll_game_sporttype:
                gameListBean=mGameListPresenter.getData();
                if(gameListBean.getType()!=null){

                    showPopwindow(v, tv_game_sporttype,setTypeList(),0);
                }
                break;
            case R.id.ll_game_area:
                gameListBean=mGameListPresenter.getData();
                if(gameListBean.getCity()!=null) {
                    showPopwindow(v, tv_game_area, setCityList(), 1);
                }
                break;

        }
    }

    private ArrayList<PopBean> setCityList() {
        list=new ArrayList<>();
        gameListBean=mGameListPresenter.getData();
        for (int i=0;i<gameListBean.getCity().size()+1;i++)
        {
            PopBean popBean=new PopBean();
            if(i==0)
            {
                popBean.setId("0");
                popBean.setName("全部地区");
            }else
            {
                popBean.setId(gameListBean.getCity().get(i-1).getId());
                popBean.setName(gameListBean.getCity().get(i-1).getName());
            }
            list.add(i,popBean);
        }
        return list;
    }
    private ArrayList<PopBean> setTypeList()
    {
        list=new ArrayList<>();
        gameListBean=mGameListPresenter.getData();
        for (int i=0;i<gameListBean.getType().size()+1;i++)
        {
            PopBean popBean=new PopBean();
            if(i==0)
            {
                popBean.setId("0");
                popBean.setName("全部运动");
            }else
            {
                popBean.setId(gameListBean.getType().get(i-1).getId());
                popBean.setName(gameListBean.getType().get(i-1).getTitle());
            }
            list.add(i,popBean);
        }
        return list;
    }

    /**
     * 弹出popwindow
     * @param v
     * @param tv
     */
    private void showPopwindow(View v,final TextView tv,ArrayList<PopBean> list,final int what)
    {
        pu=PopUtil.getInstance(this,v,width,width,list);
        pu.setListener(new PopUtil.OnDatePopClickListener()
        {
            @Override
            public void onClick(PopUtil pu, PopBean date)
            {
                tv.setText(date.getName());
                pu.dismiss();
                if(0==what)
                {
                    type_id=date.getId();
                    page=1;
                    mGameListPresenter.getGameList(page+"",type_id,city_id);
                }else
                {
                    page=1;
                    city_id=date.getId();
                    mGameListPresenter.getGameList(page+"",type_id,city_id);
                }
            }
        });
        pu.show();
    }

    @Override
    public int getmyPage()
    {
        return page;
    }

    @Override
    public XListView getListView()
    {
        return lv_game_list;
    }

    @Override
    public GameListPresenter getGameListPresenter()
    {
        mGameListPresenter=new GameListPresenter(this,this,this);
        return mGameListPresenter;
    }

    @Override
    public LinearLayout getllRefresh() {
        return llRefresh;
    }

    @Override
    public void finishActivity()
    {
        this.finish();
    }
    public void onLoad()
    {
        lv_game_list.stopRefresh();
        lv_game_list.stopLoadMore();
        lv_game_list.setRefreshTime("");
    }

    @Override
    public LinearLayout getNoContent()
    {
        return llnoContent;
    }

    @Override
    public void onRefresh()
    {
        page=1;
        lv_game_list.setPullLoadEnable(true);
        mGameListPresenter.getGameList(page+"",type_id,city_id);
    }

    @Override
    public void onLoadMore()
    {
            page++;
            mGameListPresenter.getGameList(page+"",type_id,city_id);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        GameListBean.Data data= (GameListBean.Data) parent.getAdapter().getItem(position);
        Intent intent=new Intent(this,GameDetailActivity.class);
        intent.putExtra("game_id",data.getId());
        intent.putExtra("title",data.getTitle());
        startActivity(intent);
    }
}
